function binwrite(filename,content)
% binwrite  - writes a binary stream from a char/uint8 array to file
%
% FORMAT:       binwrite(filename,content)
%
% Input Fields:
%       filename    name to a file, preferably absolute path
%       content     char/uint8 array to write
%
% See also asciiwrite

% Version:  v0.7f
% Build:    8110521
% Date:     Nov-05 2008, 9:00 PM CET
% Author:   Jochen Weber, SCAN Unit, Columbia University, NYC, NY, USA
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% argument check
if nargin < 2 || ...
  ~ischar(filename) || ...
    isempty(filename) || ...
    (~ischar(content) && ...
     ~isa(content, 'uint8'))
    error( ...
        'BVQXtools:BadArgument', ...
        'Bad or missing arguments in call.' ...
    );
end

% filename mangling check
if ispc
    filename = strrep(filename(:)', '/', filesep);
else
    filename = strrep(filename(:)', '\', filesep);
end

% open file and check for handle
ofp = fopen(filename, 'w');
if ofp < 1
    error( ...
        'BVQXtools:FileNotWritable', ...
        'Couldn''t write to file: %s.', ...
        filename ...
    );
end
frewind(ofp);

% rewind file, write content, and close file
if ischar(content)
    fwrite(ofp, content, 'uchar');
else
    fwrite(ofp, content, 'uint8');
end
fclose(ofp);
